//
//  MainClassTableViewController.m
//  B项目
//
//  Created by 陶志福 on 15/12/17.
//  Copyright © 2015年 tzf. All rights reserved.
//

#import "MainClassTableViewController.h"
#import "MainClassTableViewCell.h"
#import "MainClassDetail.h"
#import "MJRefresh.h"
#import "StrategyDetaiViewController.h"
#import "WebViewController.h"
@interface MainClassTableViewController ()

@property (nonatomic,strong) NSMutableArray *detailArray;


@property (nonatomic,strong) NSMutableArray *modelArray;

@end

static NSInteger num = 0;

@implementation MainClassTableViewController


//- (NSMutableArray *)detailArray
//{
//    if (!_detailArray) {
//        _detailArray = [[NSMutableArray alloc] init];
//    }
//    return _detailArray;
//}
- (NSMutableArray *)modelArray{
    if (!_modelArray) {
        _modelArray = [[NSMutableArray alloc]init];
    }
    return _modelArray;
}

- (void)jsonWithDetail
{
    if ([_type isEqualToString:@"搜索框"]) {
        //转换成 utf8编码格式
        NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)self.ID, NULL, NULL,  kCFStringEncodingUTF8 ));
        
        NSString *urlString = [[kStrategy1 stringByAppendingString:encodedString]stringByAppendingString:kStrategy2];
        NSLog(@"%@",urlString);
        
        [LORequestManger GET:urlString success:^(id response) {
            
            NSDictionary *Dic = (NSDictionary *)response;
            
            NSArray *titleArray = Dic[@"data"][@"posts"];
            [_detailArray removeAllObjects];
            for (NSDictionary *dic2 in titleArray) {
                MainClassDetail *mainDetail = [[MainClassDetail alloc] init];
                [mainDetail setValuesForKeysWithDictionary:dic2];
                
                [_detailArray addObject:mainDetail];
               
                
            }
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }
    else
    {
        if ([_first isEqualToString:@"1"]) {
            _first = @"2";
        }else
        {
            num = 0;
        }
        NSString *url = [NSString stringWithFormat:kDetail,self.ID,num];
        if ([_type isEqualToString:@"Special"]) {
            url = [NSString stringWithFormat:kSpecialDedail,self.ID];
        }
        [LORequestManger GET:url success:^(id response) {
            NSDictionary *Dic = (NSDictionary *)response;
            NSDictionary *SubDic = Dic[@"data"][@"items"];
            
            if ([_type isEqualToString:@"Special"]) {
                SubDic = Dic[@"data"][@"posts"];
            }
            if ([_first isEqualToString:@"2"]) {
                _first = nil;
            }
            else
            {
                [_detailArray removeAllObjects];
            }
            for (NSDictionary *dic2 in SubDic) {
                MainClassDetail *mainDetail = [[MainClassDetail alloc] init];
                
                [mainDetail setValuesForKeysWithDictionary:dic2];
                
                [_detailArray addObject:mainDetail];
                
                [self.modelArray addObject:mainDetail];

                           }

            NSLog(@"%ld  %ld",[_detailArray count],num);
            [self.tableView reloadData];
            [self.tableView headerEndRefreshing];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    _detailArray = [[NSMutableArray alloc] init];
    [self jsonWithDetail];
    [self.tableView registerClass:[MainClassTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak MainClassTableViewController *mainTVC = self;
    [self.tableView addHeaderWithCallback:^{
        [mainTVC jsonWithDetail];
    }];
    if ([_type isEqualToString:@"搜索框"]) {
    }
    else
    {
        [self.tableView addFooterWithCallback:^{
            _first = @"1";
            num += 20;
            [mainTVC jsonWithDetail];
            [mainTVC.tableView footerEndRefreshing];
        }];
    }
   
    
    
    
    
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 170;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.detailArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MainClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    MainClassDetail *mainDetail = _detailArray[indexPath.row];
    cell.mainDetail = mainDetail;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_type isEqualToString:@"搜索框"]) {
        WebViewController *web = [[WebViewController alloc]init];
        MainClassDetail *mainDetail = _detailArray[indexPath.row];
        web.mainClassDetail = self.detailArray[indexPath.row];
        
        web.userid = [NSString stringWithFormat:kSearchDetail,mainDetail.ID];
        [web setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:web animated:YES];
        web.type = 2;
	
        
    }else{
        
        WebViewController *web = [[WebViewController alloc]init];
        MainClassDetail *mainDetail = _detailArray[indexPath.row];
        web.mainClassDetail = self.modelArray[indexPath.row];
        
        web.userid = [NSString stringWithFormat:kSearchDetail,mainDetail.ID];
        web.imageStr = mainDetail.cover_image_url;
        web.titleS = mainDetail.title;
        
        [web setHidesBottomBarWhenPushed:YES];
        [self.navigationController pushViewController:web animated:YES];
        web.type = 2;
        
    }
}

//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    //转动特效
//    CATransform3D rotation;
//    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
//    rotation.m34 = 1.0/ -500;
//    
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    cell.alpha = 0;
//    cell.layer.transform = rotation;
//    cell.layer.anchorPoint = CGPointMake(0, 0.5);
//    
//    [UIView beginAnimations:@"rotation" context:NULL];
//    [UIView setAnimationDuration:1.0];
//    cell.layer.transform = CATransform3DIdentity;
//    cell.alpha = 1;
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    [UIView commitAnimations];
//    
//    [UIView animateWithDuration:1.0 animations:^{
//        cell.layer.transform = CATransform3DIdentity;
//        cell.alpha = 1;
//        cell.layer.shadowOffset = CGSizeMake(0, 0);
//    }];
//}




@end
